#include "time.h"
#include <muduo/base/Logging.h>
#include <muduo/net/Endian.h>
#include <boost/bind.hpp>

using namespace muduo;
using namespace muduo::net;

TimeServer::TimeServer(EventLoop *loop, const InetAddress &listenAddr) 
	: server_(loop, listenAddr, "TimeServer") {
	server_.setConnectionCallback(boost::bind(&TimeServer::OnConnection, this, _1));
	server_.setMessageCallback(boost::bind(&TimeServer::OnMessage, this, _1, _2, _3));
}

void 
TimeServer::Start() {
	server_.start();
}

void
TimeServer::OnConnection(const TcpConnectionPtr &conn) {
	LOG_INFO << "TimeServer - " << conn->peerAddress().toIpPort() << " -> "
	         << conn->localAddress().toIpPort() << " is "
	         << (conn->connected() ? "UP" : "DOWN");
	if (conn->connected()) {
		time_t now = ::time(NULL);
		int32_t be32 = sockets::hostToNetwork32(static_cast<int32_t>(now));
		conn->send(&be32, sizeof(be32));
		conn->shutdown();
	}
}

void
TimeServer::OnMessage(const TcpConnectionPtr &conn, Buffer *buf, Timestamp time) {
	string msg(buf->retrieveAllAsString());
	LOG_INFO << conn->name() << " discards " << msg.size()
	         << " bytes received at " << time.toString();
}
